package org.bronycorps.mlptss.screen;

import org.bronycorps.mlptss.domain.controller.GameController;

import aurelienribon.tweenengine.BaseTween;
import aurelienribon.tweenengine.Tween;
import aurelienribon.tweenengine.TweenCallback;
import aurelienribon.tweenengine.TweenManager;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasRegion;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;

public class GameOverScreen extends AbstractMenuScreen {

	private TextureAtlas atlas;
	private Image numberImage;
	private TweenManager tweenManager;

	public GameOverScreen() {
		super();

		this.atlas = this.manager.get(
				this.config.convertPath(this.menuConfig.getGameOverAtlas()),
				TextureAtlas.class);
		this.tweenManager = new TweenManager();
	}

	@Override
	public void render(float delta) {
		this.tweenManager.update(delta);

		super.render(delta);
	}

	private void setNumber(int number) {
		AtlasRegion numberRegion = this.atlas
				.findRegion(String.valueOf(number));
		this.numberImage.setDrawable(new TextureRegionDrawable(numberRegion));
		this.numberImage.setPosition((LevelScreen.STAGE_WIDTH / 2)
				- numberRegion.getRegionWidth() / 2,
				(LevelScreen.STAGE_HEIGHT / 2) - numberRegion.getRegionHeight()
						/ 2);
	}

	@Override
	public void show() {
		Gdx.input.setInputProcessor(new InputProcessor() {

			@Override
			public boolean keyDown(int keycode) {
				return false;
			}

			@Override
			public boolean keyTyped(char character) {
				return false;
			}

			@Override
			public boolean keyUp(int keycode) {
				switch (keycode) {
				case Input.Keys.ESCAPE:
					GameController.getInstance().loadMainMenu();
					break;
				case Input.Keys.SPACE:
				case Input.Keys.ENTER:
				case Input.Keys.CONTROL_LEFT:
					// continue game
					GameController.getInstance().continueGame();
					break;
				}

				return true;
			}

			@Override
			public boolean mouseMoved(int screenX, int screenY) {
				return false;
			}

			@Override
			public boolean scrolled(int amount) {
				return false;
			}

			@Override
			public boolean touchDown(int screenX, int screenY, int pointer,
					int button) {
				return false;
			}

			@Override
			public boolean touchDragged(int screenX, int screenY, int pointer) {
				return false;
			}

			@Override
			public boolean touchUp(int screenX, int screenY, int pointer,
					int button) {
				return false;
			}
		});

		// put background image
		AtlasRegion backgroundRegion = this.atlas.findRegion("background");
		Image bgImage = new Image(backgroundRegion);
		this.stage.addActor(bgImage);

		// add countdown number
		AtlasRegion numberRegion = this.atlas.findRegion("9");
		this.numberImage = new Image(numberRegion);
		this.numberImage.setPosition((LevelScreen.STAGE_WIDTH / 2)
				- numberRegion.getRegionWidth() / 2,
				(LevelScreen.STAGE_HEIGHT / 2) - numberRegion.getRegionHeight()
						/ 2);
		this.stage.addActor(this.numberImage);

		for (int i = 1; i < 9; i++) {
			final int count = i;
			Tween.call(new TweenCallback() {

				@Override
				public void onEvent(int type, BaseTween<?> source) {
					setNumber(9 - count);

				}
			}).delay(i).start(this.tweenManager);
		}
		Tween.call(new TweenCallback() {

			@Override
			public void onEvent(int type, BaseTween<?> source) {
				GameController.getInstance().loadMainMenu();
			}
		}).delay(9).start(this.tweenManager);
	}

}
